import redis
import json
from data.redis_data import *


class PyRedis:
    _conn = None

    @classmethod
    def get_conn(cls, DB):
        if cls._conn is None:
            cls._conn = redis.Redis(host, Port, DB, password, decode_responses=True)
        return cls._conn

    @classmethod
    def close_conn(cls):
        """关闭数据库连接"""
        if cls._conn:
            cls._conn.close()
            cls._conn = None

    @classmethod
    def get_value(cls, DB, key, value):
        conn = cls.get_conn(DB)
        try:
            if not conn.exists(key):
                conn.set(key, json.dumps(value))
        finally:
            cls.close_conn()
        return json.loads(conn.get(key))


if __name__ == '__main__':
    # print(PyRedis().get_value(supplier.get("DB"), supplier.get("supplier_key"), supplier.get("supplier_value")))
    print(PyRedis().get_value(minimall.get("DB"), minimall.get("minimall_key"), minimall.get("minimall_value")))
